// miniprogram/pages/my/my.js
let app = getApp();
import requestApi from '../../common/requestApi.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    order_list: [{
        name: '全部订单',
        img: '../../images/or1.png'
      },
      {
        name: '待付款',
        img: '../../images/or2.png'
      },
      {
        name: '待发货',
        img: '../../images/or3.png'
      },
      {
        name: '待收货',
        img: '../../images/or4.png'
      },
      {
        name: '退款/售后',
        img: '../../images/or5.png'
      },
    ],
    user_credit: [{
        txt: '积分',
        num: 0
      },
      {
        txt: '优惠券',
        num: 1
      },
      {
        txt: '礼品卡',
        num: 4
      },
      {
        txt: '赠品',
        num: 1
      }
    ],
    site_list: [{
        img: '../../images/my2.png',
        name: '收货地址'
      },
      {
        img: '../../images/my3.png',
        name: '个人资料'
      },
      {
        img: '../../images/my4.png',
        name: '我的收藏'
      }, {
        img: '../../images/my6.png',
        name: '超级管理员'
      }
    ]
  },
  goTicket() {
    wx.navigateTo({
      url: '../applications/ticket/ticket',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.getMyUserInfo();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },
  getMyUserInfo() {
    let _this = this;
    let db = wx.cloud.database();
    wx.cloud.callFunction({
      name: 'login',
      success: res => {
        let openid = res.result.openid;
        let url = app.globalData.requestUrl + 'users/getUserInfo',
            method = "POST",
          datas = { openid };
        requestApi.api(url,method,datas)
        .then(res=>{
          console.log(res)
          _this.setData({
            uinfo:res.data.data
          })
        })
        .catch(err=>{
          wx.showLoading({
            title: '网络错误',
          })
        })
      },
      fail: err => {
        console.log(err)
      }
    })
  },
  getPhoneNumber(e) {
    console.log(e);
    let uinfo = this.data.uinfo;
    let _this = this;
    let db = wx.cloud.database();
    if (e.detail.errMsg == 'getPhoneNumber:fail user deny') {
      return wx.showToast({
        title: '您拒绝了绑定',
      })
    }
    let enData = e.detail.encryptedData;
    let iv = e.detail.iv;
    wx.login({
      success: res => {
        console.log(res.code);
        if (res.code) {
          wx.cloud.callFunction({
            name: 'getSessionKey',
            data: {
              code: res.code,
              enData: enData,
              iv: iv
            },
            success: resp => {
              console.log(resp)
              let phone = resp.result.phoneNumber;
              wx.cloud.callFunction({
                name: 'login',
                success: rep => {
                  console.log(rep);
                  let openid = rep.result.openid;
                  db.collection('tb_user_list').where({
                    _openid: openid
                  }).get({
                    success: resr => {
                      let id = resr.data[0]._id;
                      db.collection('tb_user_list').doc(id).update({
                        data: {
                          user_phone: phone
                        },
                        success: ress => {
                          wx.showToast({
                            title: '绑定成功',
                          })
                          _this.getMyUserInfo();
                        },
                        fail: erss => {
                          wx.showToast({
                            title: '绑定失败',
                          })
                        }
                      })
                    },
                    fail: errr => {
                      wx.showToast({
                        title: '网络错误',
                      })
                    }
                  })

                },
                fail: erp => {
                  wx.showToast({
                    title: '网络错误',
                  })
                }
              })
            },
            fail: err => {
              wx.showToast({
                title: '网络错误',
              })
            }
          })
        } else {
          wx.showToast({
            title: '登录失败',
          })
        }
      }
    })
  },
  goAllOrder() {
    wx.navigateTo({
      url: '../applications/order/order',
    })
  },
  goOtherViews(e) {
    let idx = e.currentTarget.dataset.idx;
    if (idx == 0) {
      wx.navigateTo({
        url: '../applications/address/address',
      })
    } else if (idx == 1) {

    } else if (idx == 2) {
      wx.navigateTo({
        url: '../applications/collect/collect',
      })
    } else {
      wx.navigateTo({
        url: '../applications/admin/admin',
      })
    }
  },
  goOrderList(e) {
    let id = e.currentTarget.dataset.idx;
    console.log(id);
    wx.navigateTo({
      url: '../applications/order/order?id=' + id,
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})